package com.huaji.learn.ui.me;

import static androidx.core.app.ActivityCompat.finishAffinity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.huaji.learn.R;
import com.huaji.learn.repository.NoteRepository;

/**
 * 我的 Fragment
 */
public class MeFragment extends Fragment {


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        // 获取Activity的ActionBar
        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        View view = inflater.inflate(R.layout.fragment_me, container, false);

        // 设置Up按钮是否可见
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
        }

        Button btnExit = view.findViewById(R.id.btnExit);
        Button btnClearData = view.findViewById(R.id.btnClearData);

        btnExit.setOnClickListener(v -> finishAffinity(getActivity()));
        btnClearData.setOnClickListener(v -> this.clearData());

        return view;
    }

    void clearData() {
        new NoteRepository(getContext()).clearAllData();
    }
}